package com.wkw.jkyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wkw.jkyf.mapper.DruginfoMapper;
import com.wkw.jkyf.pojo.Druginfo;
import com.wkw.jkyf.pojo.Supplier;
import com.wkw.jkyf.service.IDruginfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

//药品
@Service
public class DruginfoServiceImpl extends ServiceImpl<DruginfoMapper, Druginfo> implements IDruginfoService {
    @Resource
    private DruginfoMapper druginfoMapper;

    @Override
    //分页查询
    public IPage<Druginfo> selectDruginfoPage(int pageNum, int pageSize, String param) {
        QueryWrapper<Druginfo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(param)){
            queryWrapper.like("name",param);
        }
        queryWrapper.orderByDesc("id");
        Page<Druginfo> page = new Page<>(pageNum,pageSize);
        return druginfoMapper.selectPage(page,queryWrapper);
    }

    @Override
    public int addDruginfo(Druginfo druginfo) {
        String string = UUID.randomUUID().toString();
        String uuid = string.substring(0,7);
        druginfo.setNumber(uuid);
        druginfo.setStock(0);
        return druginfoMapper.insert(druginfo);
    }

    @Override
    public int editDruginfo(Druginfo druginfo) {
        return druginfoMapper.updateById(druginfo);
    }

    @Override
    public Druginfo queryDruginfoById(Integer id) {
        return druginfoMapper.selectById(id);
    }

    @Override
    public int deleteDruginfoById(Integer id) {
        return druginfoMapper.deleteById(id);
    }

    @Override
    public List<Druginfo> queryDruginfoList() {
        return druginfoMapper.selectList(null);
    }

    @Override
    public List<Druginfo> selectDrugNumberByName(String drugName,String number) {
        if(!StringUtils.isEmpty(drugName)){
            QueryWrapper<Druginfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name",drugName);
            return druginfoMapper.selectList(queryWrapper);
        }
        if(!StringUtils.isEmpty(number)){
            QueryWrapper<Druginfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("number",number);
            return druginfoMapper.selectList(queryWrapper);
        }
       return null;
    }
}
